//
//  ZCPublishController.swift
//  weibo
//
//  Created by 张城 on 16/7/26.
//  Copyright © 2016年 张城. All rights reserved.
//

import UIKit

class ZCPublishController: UIViewController {
    
    private lazy var textView: ZCTextView = {
       let tv = ZCTextView()
        tv.delegate = self
        tv.font = UIFont.systemFontOfSize(18)
        tv.textColor = UIColor.whiteColor()
        
        tv.alwaysBounceVertical = true
        tv.placeholder = "分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事...分享新鲜事..."
        return tv
    }()
    
    private lazy var titleView: UILabel = {
       let name = ZCuserAccountViewModel.sharedTools.userAccount?.screen_name ?? ""
        let title = "发微博\n\(name)"
        let range = (title as NSString).rangeOfString(name)
        let attr = NSMutableAttributedString(string: title)
        attr.addAttributes([NSFontAttributeName:UIFont.systemFontOfSize(13),NSForegroundColorAttributeName:UIColor.magentaColor()], range: range)
        let label = UILabel()
        label.numberOfLines = 0
        label.attributedText = attr
        label.textAlignment = .Center
        label.sizeToFit()
        return label
    }()
    
    private lazy var picView: ZCPictureView = {
        let piv = ZCPictureView()
        
        if piv.images.count > 0 {
            self.navigationItem.rightBarButtonItem?.enabled = true
        }
        
        return piv
    }()
    private lazy var toolView: ZCToolView = ZCToolView()
    
    
    
//    override func loadView() {
//        view = textView
//    }
//    

    override func viewDidLoad() {
        super.viewDidLoad()
//  printLog(44444)
        setUpNav()
    }
    
    func setUpNav() {
        
        navigationItem.titleView = titleView
        
        navigationItem.leftBarButtonItem = ZCBarButtonItem(title: "取消", imgName: nil, target: self, action: #selector(ZCPublishController.clickCancel))
        
        navigationItem.rightBarButtonItem = ZCBarButtonItem(title: "发布", imgName: nil, target: self, action: #selector(ZCPublishController.clickPublish))
        
        navigationItem.rightBarButtonItem?.enabled = false
        
        view.addSubview(textView)
        textView.addSubview(picView)
        view.addSubview(toolView)
        
        
        textView.snp_makeConstraints { (make) in
            make.edges.equalTo(view)
        }
        
        picView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSizeMake(SCREENW - 40, SCREENW - 40))
            make.centerX.equalTo(textView.snp_centerX)
            make.top.equalTo(150)
        }
        
        picView.closureImage = {
            
            self.choosePicture()
        }
        
        toolView.snp_makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(view)
            make.height.equalTo(44)
        }
        
        toolView.closure = {(type: ZCToolViewButtonType)->() in
            
            switch type {
            case .Picture:
//                print("图片")
                self.choosePicture()
            case .Mention:
                print("@")
            case .Trend:
                print("#")
            case .Emoticon:
                print("😊")
            case .Add:
                print("+")
            
            }
        }
    }
    
    func clickCancel() {
        self.view.endEditing(true)
        dismissViewControllerAnimated(false, completion: nil)
    }

    func clickPublish() {
        print("有内容")
    }
    
}


extension ZCPublishController : UITextViewDelegate {
    
    func textViewDidChange(textView: UITextView) {
        
        navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        self.textView.resignFirstResponder()
    }
    
}



extension ZCPublishController{
    func choosePicture() {
        
        let picker = UIImagePickerController()
        picker.delegate = self;
        self.presentViewController(picker, animated: true, completion: nil)
        
    }
}

extension ZCPublishController : UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        
        picView.addImage(image.getImageScale(400))
        
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
}
















